<template>
    <div class="login-wrap">
        <el-form class="login-form" label-position="top" label-width="80px" :model="formdata">
          <el-form-item label="用户名">
            <el-input v-model="formdata.username"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input type="password" v-model="formdata.password"></el-input>
          </el-form-item>
          <el-button @click.prevent="handleLogin()" class="login-btn" type="primary">登录</el-button>
        </el-form>
    </div>
</template>

<script>
     export default{
        data(){
            return{
                formdata:{
                    username:'',
                    password:''
                }
            }
        },
        methods:{
           async handleLogin(){
                const res = await this.$http.post('/login',this.formdata)

                        const {meta:{msg,status},data} = res.data
                        if(status ===200){
                            this.$router.push({name:'home'})
                            const token = localStorage.setItem('token', data.token)
                            this.$message.success(msg)
                        }else{
                            // 3. 如果失败 -> 提示用户
                            this.$message.warning(msg)
                        }
            }
        }
     }
</script>

<style >
  .login-wrap {
  height: 100%;
  background-color: #324152;
  display: flex;
  justify-content: center;
  align-items: center;
}
.login-wrap .login-form {
  width: 400px;
  background-color: #fff;
  border-radius: 5px;
  padding: 30px;
}
.login-wrap .login-btn {
  width: 100%;
}  
</style>